<?php 
	
	class Model extends nie{
		
		protected $table_name;
		protected $model_name;
		protected $primary_key='id';
		protected $default_order='id desc';
		protected $db;
		protected $query=array(
			'action'=>'select',
			'fields'=>'*',
			'table'=>'',
			'values'=>'',
			'limit'=>'',
			'order'=>'',
			'condition'=>'',
			'values'=>''
		);
		
		public function __construct(){
		
			$this->db=new DB(DB_NAME,DB_HOST,DB_USER,DB_PW);
			
			switch(func_num_args()){			
				case 1:
					$this->model_name=func_get_arg(0);
					$this->table_name=DB_TABLE_PREFIX.$this->model_name;
					break;
				case 2:
					$this->model_name=func_get_arg(0);
					$this->table_name=DB_TABLE_PREFIX.func_get_arg(1);
					break;
				case 3:
					$this->model_name=func_get_arg(0);
					$this->table_name=DB_TABLE_PREFIX.func_get_arg(1);
					$this->primary_key=func_get_arg(2);
					$this->default_order=$this->primary_key.' desc';
					break;
				default:break;
			}
			
		}
		
		public function clearquery(){
		
			$this->query=array(
				'action'=>'select',
				'fields'=>'*',
				'table'=>'',
				'values'=>'',
				'limit'=>'',
				'order'=>'',
				'condition'=>'',
				'values'=>''
			);
		
		}
		
		public function get(){
		
			$this->clearquery();
			
			switch(func_num_args()){
				
				case 0: //return an object template;
					if(property_exists(__CLASS__,'instance')){
						return $this->instance;
					}
					break;
				case 1: //return one;
					$arg=func_get_arg(0);
					if($arg=='new'){}
					elseif($arg=='last'){}
					elseif(preg_match('/^top([0-9]+)/',$arg,$matches)){
						return $this->limit($matches[1])->select();
					}elseif(preg_match('/^([0-9]+),([0-9]+)$/',$arg,$matches)){
						return $this->limit(($matches[1]-1)*$matches[2].','.$matches[2])->select();
					}else{
						//return where PRIMARY =;
						return $this->where('id='.func_get_arg(0))->select();
					}
					break;
				case 2: //return one page;
					$arg1=func_get_arg(0);
					$arg2=func_get_arg(1);
					if(preg_match('/^top([0-9]+)/',$arg1,$matches)){
						return $this->limit($matches[1])->where($arg2)->select();
					}elseif(preg_match('/^([0-9]+),([0-9]+)$/',$arg1,$matches)){
						return $this->limit(($matches[1]-1)*$matches[2].','.$matches[2])->where($arg2)->select();
					}
					break;
				default:break;
			
			}
		}
		
		private function queryString(){
			switch(strtoupper($this->query['action'])){
				case 'SELECT':
					return implode(' ',array(
						'SELECT',
						$this->query['fields'],
						'From',
						$this->table_name,
						$this->query['condition']==''?'':'Where '.$this->query['condition'],
						'order by',
						$this->query['order']==''?$this->default_order:$this->query['order'],
						$this->query['limit']==''?'':'LIMIT '.$this->query['limit']
					));
				break;
				case 'INSERT':
					return implode(' ',array(
						'INSERT INTO',
						$this->table_name,
						'('.$this->query['fields'].')',
						'VALUES',
						'('.$this->query['values'].')'
					));					
				break;
				case 'UPDATE':
					
					$fields=explode(',',$this->query['fields']);
					
					$values=explode(',',$this->query['values']);
					
					$valuestring=array();
					
					for($i=0,$j=count($fields);$i<$j;$i++){
						
						$valuestring[]=$fields[$i].'='.$values[$i];
						
					}
					
					return implode(' ',array(
						'UPDATE',
						$this->table_name,
						'SET',
						implode(',',$valuestring),
						'WHERE',
						$this->query['condition']
					));	
				break;
				case 'DELETE':
					return implode(' ',array(
						'DELETE FROM',
						$this->table_name,
						'WHERE',
						$this->query['condition']
					));
				break;
			}
		}
		public function delete($where){
			$this->query['action']='delete';
			$this->query['condition']=$where;
			return $this->db->query($this->queryString());		
		}
		public function update($where,$data){
			$this->query['action']='update';
			$this->query['fields']=implode(',',array_keys($data));
			$this->query['values']='"'.implode('","',array_values($data)).'"';
			$this->query['condition']=$where;
			return $this->db->query($this->queryString());
					
		}
		
		public function put($data){
			$this->query['action']='insert';
			$this->query['fields']='`'.implode('`,`',array_keys($data)).'`';
			$this->query['values']='"'.implode('","',array_values($data)).'"';
			$this->db->query($this->queryString());
			return $this->db->lastInsertedId();
		}
		
		public function select($fields='*'){
		
			$this->query['action']='select';
			
			$this->query['fields']=$fields;
			
			$this->db->query($this->queryString());
				
			return $this->db->fetchAll();
				
			
		}
		public function where($where){
			$this->query['condition']=$where;
			return $this;
		}
		public function order($order){
			$this->query['order']=$order;
			return $this;
		}
		public function limit($limit){
			$this->query['limit']=$limit;
			return $this;
		}
		
		public function total($where=''){
		
			return $where==''?$this->db->countOfAll($this->table_name):$this->db->countOf($this->table_name,$where);
		}
				
		public function __destruct(){}
	
	}
	
?>